// auction/order/order.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    site_checked: 0,
    pay_way: false,
    payment: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.orderData(options.id)

  },

  orderData(e) {
    var _this = this
    getApp().request({
      url: getApp().api.auction.submit_preview,
      method: "GET",
      data: {
        bidid: e
      },
      success(res) {
        _this.setData({
          address: res.data.address,
          goods_info: res.data.info,
          pay_list: res.data.pay_list
        })
      },
      fail(res) {
        console.log(res)
      }
    })
  },
  clicksite(e) {
    var index = e.currentTarget.dataset.index
    this.setData({
      site_checked: index
    })
  },
  clickpayway() {
    this.setData({
      pay_way: true
    })
  },
  waychecked(e) {
    this.setData({
      payment: e.currentTarget.dataset.payment,
      pay_way: false
    })
  },
  SubmitOrder() {
    var _this = this
    getApp().request({
      url: getApp().api.auction.submit,
      method: "POST",
      data: {
        address_id: _this.data.address[_this.data.site_checked].id,
        payment: _this.data.payment,
        order_id: _this.data.goods_info.bid_id,
      },
      success(res) {
        getApp().request({
          url: getApp().api.auction.pay_data,
          method: "POST",
          data: {
            order_id: res.data.order_id,
            pay_type: _this.data.payment == 0 ?
              "WECHAT_PAY" : _this.data.payment == 3 ?
              "BALANCE_PAY" : "",
            is_api: 1
          },
          success(res) {
            console.log(_this.data.payment)
            if (_this.data.payment == 0) {
              wx.requestPayment({
                timeStamp: res.data.timeStamp,
                nonceStr: res.data.nonceStr,
                package: res.data.package,
                signType: 'MD5',
                paySign: res.data.paySign,
                success(res) {
                  console.log(res)
                  _this.setData({
                    pay_way: false,
                    bid_show: true
                  })

                },
                fail(res) {
                  console.log(res)
                }
              })
            }
          
          },

        })

      },

    })
  }
})